package com.httpclient;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * httpclient 测试类
 *  HttpClientTest.java
 * 
 * @author yaojinshan
 * @since 2018年5月21日下午5:23:54
 */
@SuppressWarnings("all")
public class HttpClientMovieDownload {
	static int pageSize = 1;
	
	static class MovieMultipleThread implements Runnable {
		private int start;
		private int end;

		public MovieMultipleThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public int getStart() {
			return start;
		}

		public void setStart(int start) {
			this.start = start;
		}

		public int getEnd() {
			return end;
		}

		public void setEnd(int end) {
			this.end = end;
		}

		public void run() {
			Map<String, Map<Integer, String>> boxData = new HashMap<String, Map<Integer, String>>();
			try {
				String pageBody = "";
				//key : 存放 href , value: 存放title 
				Map<String,String> dataMap = new HashMap<String, String>(); 
				for (int i = start ; i <= end; i++) {
					if(i == 1){
						pageBody = HttpClientUtil.getRequest(domain , HttpClientUtil.CHARSET_GBK);
					}else{
						pageBody = HttpClientUtil.getRequest(domain + "/latest-updates/" + i + "/", HttpClientUtil.CHARSET_GBK);
					}
					buildDataMap(pageBody, dataMap);
				}
				// 循环获取详情页的数据
				for (String hrefKey : dataMap.keySet()) {
					String title = dataMap.get(hrefKey);
					File dir = new File("E:\\httpclient_data\\movie\\" + title);
					if (!dir.exists()) {
						dir.mkdir();
					}
					try {
						downloadSource(hrefKey, dir);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/**
		 * 下载资源
		 */
		private void downloadSource(String hrefKey, File dir) {
			String detailPageBody = HttpClientUtil.getRequest(hrefKey, HttpClientUtil.CHARSET_GBK);
			if(detailPageBody != null){
				Document detailDoc = Jsoup.parse(detailPageBody);
				if(detailDoc != null){
					Element infoEle = detailDoc.selectFirst(".info");
					Element lastItemDiv = infoEle.select(".item").last();
					Element aEle = lastItemDiv.getElementsByTag("a").get(0);
					//System.out.println(aEle.attr("href"));
					String resourceName = aEle.attr("href").substring(aEle.attr("href").lastIndexOf("=") + 1);
					System.out.println("开始下载：" + resourceName + ",下载路径：" + dir.getPath());
					HttpClientUtil.downloadMovie(aEle.attr("href"), resourceName, dir);
					System.out.println("下载完成：" + resourceName + ",下载路径：" + dir.getPath());
				}
			}
		}

		private void buildDataMap(String pageBody, Map<String, String> dataMap) {
			if(pageBody == null || "".equals(pageBody)){
				return;
			}
			Document doc = Jsoup.parse(pageBody);
			//String title = doc.getElementsByTag("title").first().text();
			String url = "";
			//Element vedioDiv = doc.getElementById("list_videos_most_recent_videos_items");//获取到box对象
			Elements itemDivs = doc.select(".item");//获取到box对象
			//获取到所有的a节点 格式为：<a	href="xxx/download_filename=angelo-godshack-pd-chloe-2.mp4" data-attach-session="PHPSESSID">MP4, 56.8 Mb</a>
			for (Element divEle : itemDivs) {
				Element aEle = divEle.getElementsByTag("a").first();
				dataMap.put(aEle.attr("href"),aEle.attr("title"));
			}
		}

	}
	public static void main(String[] args) {
		try {
//			String body = HttpClientUtil.getRequest("http://www.sexx2019.com/latest-updates/2/", HttpClientUtil.CHARSET_GBK);
			String body = HttpClientUtil.getRequest(domain, HttpClientUtil.CHARSET_GBK);
			//String body = HttpClientUtil.getRequest(HttpClientUtil.domain + "/list/index3.html", HttpClientUtil.CHARSET_GBK);
			//System.out.println(body);
			if(body != null && !"".equals(body)){
				Document rootDoc = Jsoup.parse(body);
				int pageNum = 0;//总页数
				//int maxPageNum = getMaxPageNum(rootDoc);//1154 页 
				int maxPageNum = 2;//1154 页 
				System.out.println(maxPageNum);
				if(maxPageNum % pageSize == 0){
					pageNum = maxPageNum / pageSize;
				}else{
					pageNum = maxPageNum / pageSize + 1;
				}
				for (int pageIndex = 0; pageIndex < pageNum; pageIndex++) {
					if(pageIndex == 0){
						new Thread(new MovieMultipleThread(1, pageSize * (pageIndex + 1))).start();
					}else if (pageIndex != 0 && pageIndex < (pageNum - 1)){
						new Thread(new MovieMultipleThread(pageSize * pageIndex + 1, pageSize * (pageIndex + 1))).start();
					}else if(pageIndex == pageNum - 1){
						new Thread(new MovieMultipleThread(pageSize * pageIndex + 1, maxPageNum)).start();
					}
				}
				System.out.println("线程全部启动完毕，启动线程个数：" + pageNum);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static int getMaxPageNum(Document doc) {
		Elements paginationElements = doc.select(".pagination-holder");
		Element lastLiElement = paginationElements.get(0).selectFirst(".last");
		String href = lastLiElement.getElementsByTag("a").get(0).attr("href");
		// 截取 字符串/latest-updates/1154/
		href = href.substring(0,href.length() - 1);
		return Integer.parseInt(href.substring(href.lastIndexOf("/") + 1));
	}
	private static final String domain = "http://www.sexx2019.com";
}
